package com.csu.cphone.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import com.csu.cphone.dao.RecycleProblemMapper;
import com.csu.cphone.dao.RecycleProblemSelectMapper;
import com.csu.cphone.pojo.RecycleProblemPojo;
import com.csu.cphone.pojo.RecycleProblemSelectPojo;
import com.csu.cphone.service.RecycleProblemSelectService;
import com.csu.cphone.service.RecycleProblemService;

/**
 * @项目名称：cphone
 * @类名称：RecycleProblemServiceImpl
 * @类描述：
 * @创建人：Tangc
 * @创建时间：2017年12月7日 下午10:01:34
 * @company:
 */
@Service
public class RecycleProblemServiceImpl implements RecycleProblemService
{
    @Autowired
    private RecycleProblemMapper recycleProblemMapper;

    @Autowired
    private RecycleProblemSelectMapper recycleProblemSelectMapper;

    @Autowired
    private RecycleProblemSelectService recycleProblemSelectService;

    /**
     * @see com.csu.cphone.service.RecycleProblemService#add(java.lang.Integer,
     *      java.lang.String, java.lang.Integer, java.util.List)
     */
    @Override
    public RecycleProblemPojo add(Integer phoneId, String problemName, Integer problemType,
            List<RecycleProblemSelectPojo> selects)
    {
        RecycleProblemPojo recycleProblem = new RecycleProblemPojo();

        recycleProblem.setPhoneId(phoneId);
        recycleProblem.setProblemName(problemName);
        recycleProblem.setProblemType(problemType);
        recycleProblemMapper.insert(recycleProblem);

        if (!CollectionUtils.isEmpty(selects))
        {
            for (RecycleProblemSelectPojo select : selects)
            {
                select.setProblemId(recycleProblem.getId());
            }
            recycleProblemSelectMapper.batchInsert(selects);
        }
        return recycleProblem;
    }

    /**
     * @see com.csu.cphone.service.RecycleProblemService#update(java.lang.Integer,
     *      java.lang.Integer, java.lang.String, java.lang.Integer)
     */
    @Override
    public Integer update(Integer id, Integer phoneId, String problemName, Integer problemType,
            List<RecycleProblemSelectPojo> selects)
    {
        RecycleProblemPojo recycleProblem = new RecycleProblemPojo();
        recycleProblem.setId(id);
        recycleProblem.setPhoneId(phoneId);
        recycleProblem.setProblemName(problemName);
        recycleProblem.setProblemType(problemType);
        Integer update = recycleProblemMapper.update(recycleProblem);
        if (!CollectionUtils.isEmpty(selects))
        {
            for (RecycleProblemSelectPojo select : selects)
            {
                Integer selectId = select.getId();
                if (selectId != null && selectId != 0)
                {

                    recycleProblemSelectService.update(select);
                }
                else
                {
                    recycleProblemSelectService.add(select.getProblemId(), select.getProblemItem(), select.getCost());
                }
            }
        }

        return update;
    }

    /**
     * @see com.csu.cphone.service.RecycleProblemService#queryById(java.lang.Integer)
     */
    @Override
    public RecycleProblemPojo queryById(Integer recycleProblemId)
    {
        RecycleProblemPojo recycleProblem = recycleProblemMapper.selectById(recycleProblemId);
        List<RecycleProblemSelectPojo> selects = recycleProblemSelectMapper.selectByProblemId(recycleProblemId);
        recycleProblem.setSelects(selects);
        return recycleProblem;
    }

    /**
     * @see com.csu.cphone.service.RecycleProblemService#count(java.lang.Integer)
     */
    @Override
    public Integer count(Integer phoneId)
    {
        if (phoneId != null && phoneId == 0)
        {
            phoneId = null;
        }
        return recycleProblemMapper.count(phoneId);
    }

    /**
     * @see com.csu.cphone.service.RecycleProblemService#queryList(java.lang.Integer,
     *      java.lang.Integer, java.lang.String, java.lang.Integer)
     */
    @Override
    public List<RecycleProblemPojo> queryList(Integer pageSize, Integer startIndex, String order, Integer recyclePhoneId)
    {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("pageSize", pageSize);
        map.put("startIndex", startIndex);
        map.put("order", order);
        if (recyclePhoneId != null && recyclePhoneId != 0)
        {
            map.put("phoneId", recyclePhoneId);
        }

        List<Integer> recycleProblemIds = new ArrayList<Integer>();
        List<RecycleProblemPojo> recycleProblemList = recycleProblemMapper.queryList(map);
        if (CollectionUtils.isEmpty(recycleProblemList))
        {
            return recycleProblemList;
        }

        // 查询问题选项
        for (RecycleProblemPojo recycleProblem : recycleProblemList)
        {
            recycleProblemIds.add(recycleProblem.getId());
        }
        List<RecycleProblemSelectPojo> selects = recycleProblemSelectMapper.selectByProblemIds(recycleProblemIds);
        Map<Integer, List<RecycleProblemSelectPojo>> selectMap = new HashMap<Integer, List<RecycleProblemSelectPojo>>();
        for (RecycleProblemSelectPojo select : selects)
        {
            Integer problemId = select.getProblemId();
            List<RecycleProblemSelectPojo> thisProblemSelects = selectMap.get(problemId);
            if (thisProblemSelects == null)
            {
                thisProblemSelects = new ArrayList<RecycleProblemSelectPojo>();
            }
            thisProblemSelects.add(select);
            selectMap.put(problemId, thisProblemSelects);
        }

        for (RecycleProblemPojo problem : recycleProblemList)
        {
            Integer problemId = problem.getId();
            problem.setSelects(selectMap.get(problemId));
        }

        return recycleProblemList;
    }

    /**
     * @see com.csu.cphone.service.RecycleProblemService#deleteById(java.lang.Integer)
     */
    @Override
    public Integer deleteById(Integer recycleProblemId)
    {
        return recycleProblemMapper.deleteById(recycleProblemId);
    }

    /**
     * @see com.csu.cphone.service.RecycleProblemService#batchAdd(java.lang.Integer,
     *      java.util.List)
     */
    @Override
    public List<RecycleProblemPojo> batchAdd(Integer phoneId, List<RecycleProblemPojo> problems)
    {
        List<RecycleProblemPojo> result = new ArrayList<RecycleProblemPojo>();
        if (!CollectionUtils.isEmpty(problems))
        {
            for (RecycleProblemPojo problem : problems)
            {
                RecycleProblemPojo problemPojo = add(phoneId, problem.getProblemName(), problem.getProblemType(),
                        problem.getSelects());
                result.add(problemPojo);
            }
        }
        return result;

    }

    /**
     * @see com.csu.cphone.service.RecycleProblemService#batchUpdate(java.lang.Integer,
     *      java.util.List)
     */
    @Override
    public Integer batchUpdate(Integer phoneId, List<RecycleProblemPojo> problems)
    {
        int result = 0;
        if (!CollectionUtils.isEmpty(problems))
        {
            for (RecycleProblemPojo problem : problems)
            {
                result += update(problem.getId(), phoneId, problem.getProblemName(), problem.getProblemType(),
                        problem.getSelects());
            }
        }
        return result;
    }

}
